package fms.dao;

import fms.model.CptSend;
import org.apache.log4j.Logger;


/**
 * FPN/RI:DA:ZJHK:AA:ZBAA:F:LH.R339..BH.R339..BHY.H82..LBN.R343..LKO.A461..HG+ZB
 * (FPN/RI:DA:起飞机场:AA:目的机场:F:航路走向) 
 * */

public class FLPMsgBuilder {
	private static  Logger logger = Logger.getLogger(FLPMsgBuilder.class);
 

	private static final String flpRespTextHead = "FPN/RI";
 

	public static  String build(CptSend cptSend) {
	  logger.info("飞行计划组报开始");
		StringBuffer responseFreeText = new StringBuffer();
		try {
			responseFreeText.append(flpRespTextHead);
			responseFreeText.append(":DA");
			responseFreeText.append(":");
			responseFreeText.append(cptSend.getDep());
			responseFreeText.append(":AA");
			responseFreeText.append(":");
			responseFreeText.append(cptSend.getArr());
			responseFreeText.append(":F");
			responseFreeText.append(":");

			String routeLine = cptSend.getRouteline();
			// ZJHK-LH/R339/BHY/H82/LBN/R343/LKO/A461/HG+ZB-ZBAA
			//LH.R339..BHY.H82..LBN.R343..LKO.A461..HG+ZB
			String[] routeArr = routeLine.split("\\/");

			for (int i = 0; i < routeArr.length; i++) {
				if (i == 0) {
					String[] route1 = routeArr[i].split("-");
					if(route1.length==1){
						continue;
					}
					else if(route1.length<=2&&route1.length>1){
						responseFreeText.append(route1[1]);
						if (i % 2 == 0) {
							responseFreeText.append(".");
						} else {
							responseFreeText.append("..");
						}
					}
					else{

						for(int j=1;j<route1.length;j++){
							if(j!=route1.length-1){
								responseFreeText.append(route1[j]).append("..");
							}
							else{
								responseFreeText.append(route1[j]).append(".");
							}
						}
					}

				}else if (i == routeArr.length - 1) {
					String[] routeend = routeArr[i]
							.split("\\-");
                    if(routeend.length<=1) {
                        responseFreeText.append(routeend[0]);
                    }
					else{
						for(int j=0;j<routeend.length-1;j++){
							if(j==0){
								responseFreeText.append(routeend[j]);
							}
							else{
								responseFreeText.append(".."+routeend[j]);
							}
						}
					}
				}
				else{
                    String[] rarr = routeArr[i].split("\\-");
                    if(rarr.length<=1){
                        responseFreeText.append(routeArr[i]);
                        if (i % 2 == 0) {
                            responseFreeText.append(".");
                        } else {
                            responseFreeText.append("..");
                        }
                    }
					//如果出现航路点-航路点直飞的情况
					else{
						for(int j=0;j<rarr.length;j++){
							responseFreeText=responseFreeText.append(rarr[j]);
							if(j==rarr.length-1){
								responseFreeText=responseFreeText.append(".");
							}
							else{
								responseFreeText=responseFreeText.append("..");

							}
						}
//                        if (i % 2 == 0) {
//                            responseFreeText.append(".");
//                        } else {
//                            responseFreeText.append("..");
//                        }

                    }
				}					
			
			}
			  logger.info("飞行计划组报结束");
		} catch (Exception e) {
			//e.printStackTrace();
			logger.error(e.getMessage() + ":飞行计划组报异常！");
			return "";
		}

		return responseFreeText.toString();
	}


	public static void main(String[] args){

		CptSend cptSend=new CptSend();
		cptSend.setArr("ZBAA");
		cptSend.setDep("ZJHK");
//		cptSend.setRouteline("ZJHK-LH/R339/BHY/H82/LBN/R343/LKO/A461/HG+ZB-ZBAA");
		cptSend.setRouteline("ZSPD-ALDAP/B221/XDX/W174/FD/W172/IGDEG/W173/NIXEP-DOBGA-ZYTL");
		FLPMsgBuilder flpMsgBuilder=new FLPMsgBuilder();
		String testResult=flpMsgBuilder.build(cptSend);
		System.out.println(testResult);

	}
}